package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    /**
     * 分页显示所有角色信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Role> pageInfo = roleService.findByPage(getLoginCompanyId(), pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "system/role/role-list";
    }
    //跳转到添加页面
    @RequestMapping("toAdd")
    public String toAdd(){
        return "system/role/role-add";
    }
    //判断id是否为空，如果为空则保存
    @RequestMapping("edit")
    public String edit(Role role){
        role.setCompanyId(getLoginCompanyId());
        role.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(role.getId())){
            roleService.save(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }
    //跳转到修改页面
    @RequestMapping("toUpdate")
    public String toUpload(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "/system/role/role-update";
    }

    //查询角色信息，跳转到角色模块页面
    @RequestMapping("roleModule")
    public String roleModule(String roleId){
        Role role = roleService.findById(roleId);
        request.setAttribute("role",role);
        return "/system/role/role-module";
    }
    //获取ZTree的zNodes返回值
    @PostMapping("getZtreeNodes")
    @ResponseBody
    public List<Map<String,Object>> getZtreeNodes(String roleId) {
        //查询所有模块
        List<Module> modules = moduleService.findAll();

        //查询指定用户已有模块
        List<Module> roleModules = moduleService.findModuleByRoleId(roleId);

        List<Map<String,Object>> list = new ArrayList<>();
        System.out.println(roleModules);
        for (Module module : modules) {
            Map<String,Object> map = new HashMap<>();
            //添加map中的数据
            map.put("id",module.getId());   //模块id
            map.put("pId",module.getParentId());  //父模块id
            map.put("name",module.getName()); //模板名称

            if(roleModules.contains(module)) {
                map.put("checked",true); //默认勾选
                System.out.println("123");
            }
            //存入list集合
            list.add(map);
        }
        return list;
    }

    @RequestMapping("updateRoleModule")
    public String updateRoleModule(String roleId,String moduleIds){

        if (!StringUtils.isEmpty(moduleIds)){
            roleService.cleanRoleModuleById(roleId);
            String[] modules = moduleIds.split(",");
            for (int i = 0; i < modules.length; i++) {
                roleService.updateRoleModule(roleId,modules[i]);
            }
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping("delete")
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }
}
